<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * author	:	Tran Van Thanh
 * email	:	thanhtran@vietnambiz.com
 * date		:	30/08/2011
 */

require_once 'client'.EXT;

class Home extends Client {

	function __construct()
	{
		parent::__construct();		
		$this->load->helper(array('custom_string'));
		$this->load->model(array('home_model','questions_model','articles_model'));
        $this->load->library('pagination');
		
		$this->data['company']	= $this->home_model->get_company();
		
		$this->data['support'] 	= $this->home_model->get_support();
		
		$this->data['utilities'] = $this->home_model->get_utilities();
		
		$this->data['questions_usually']	= $this->questions_model->get_questions_usually();
	}

	function index($p = 1)
	{
	    
        $limit = 12;
        $offset = ($p-1)*$limit;
		//Get slideshows
		$this->data['slideshows']	= $this->home_model->get_slideshows();
		
		//Get news works
		$this->data['news_works']	= $this->home_model->get_news('works');
		
		//Get news
		$this->data['all_news']		= $this->home_model->get_news('all');
		
		//Get questions
		$this->data['questions']	= $this->home_model->get_questions(1);
		
		$this->data['articles_intro']	=	$this->articles_model->order_by('id','ASC')->get_many_by(array('active' => 1, 'type' => 1));
		$this->data['articles']	=	$this->articles_model->order_by('id','ASC')->get_many_by(array('active' => 1, 'type' => 0));
        
		
		//Get products HOT
		$result	= $this->home_model->get_all_products('hot',$offset,$limit);
        $this->data['products_hot'] =   $result['rows'];
        
        $url			=	site_url('');
        $total_page = ceil($result['num_rows']/$limit);		
			
		$conf = array(
			'cur_page'			=>	$p,
			'base_url'			=>	$url,
			'total_rows'		=>	$total_page,	
			'cur_class'			=>	'currentpage',
			'prev_class'		=>	'prevnext',
			'next_class'		=>	'prevnext',
			'next_link'			=>	'Tiếp theo',
			'prev_link'			=>	'Trang trước',
			'show_total'		=>	'no',
			'show_first_last'	=>	'no'
		);
		
		$this->pagination->initialize($conf);
		$this->data['pagnav'] 		= $this->pagination->non_ajax();

		//Get products new
		//$this->data['products_new']	= $this->home_model->get_all_products('all',2);
		//Get products best sales
		//$this->data['products_best_sales']	= $this->home_model->get_all_products('best_sales',2);

		//Get all partner
		$this->data['all_partner'] = $this->home_model->get_partners();
		
		$this->data['tpl_file']		=	'home/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function download_file($sid = '', $doc_ascii_name = '')
	{		
		// Check key
		if(isset($_SESSION['dl_key'])) 
		{
			$dl_key = $_SESSION['dl_key'];
			if(md5($doc_ascii_name.'_'.$_SERVER['REMOTE_ADDR'].'_'.$dl_key) != $sid) 
			{
				redirect('error_404'); exit;
			}
		}
		else {
			redirect('error_404'); exit;
		}

		$dir ="/";
		$detail = $this->home_model->get_detail_file($doc_ascii_name);
		$url = base_url();
		$url = str_replace("/","\/",$url);
		
		if(!empty($detail) && isset($_SERVER['HTTP_REFERER']) && preg_match("/($url)/",$_SERVER['HTTP_REFERER']))
		{
			$link = $detail->file_path;
		    $file = $dir.$link;

		    if(file_exists("./".$link))
		    {
		    	//		    var_dump($link); die;
			    header("Content-type: application/force-download");
			    header("Content-Transfer-Encoding: Binary");
			    header("Content-disposition: attachment; filename="."Dieuhoaxanh.com_".basename(character_limiter($file,100)));
			    
			    readfile($file);
		    } else {
		    	die("File does not exist");
		    }
		} else {
		    echo "No file selected";
		}
	}
	
	function received_letter()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST') 
		{
			$email = strip_tags($this->input->post('email'));
			if($this->questions_model->check_email_customers($email)==FALSE)
			{
				$data = array('fullname' => 'Guest','email' => $email,'date_create'		=> date('Y-m-d H:i:s'));
			 	$insert = $this->home_model->insert($data);
			 	if($insert)
			 	{redirect(base_url().'received_letter/success.html');}
			}
			else {redirect(base_url().'received_letter/error.html');}
		}
	}
	
	function received_letter_success()
	{
		$this->data['tpl_file']		=	'home/received_letter_success';
		$this->load->view('layout/default',$this->data);
	}
	function received_letter_error()
	{
		$this->data['tpl_file']		=	'home/received_letter_error';
		$this->load->view('layout/default',$this->data);
	}
    
    function cata($cat_name= null,$p=1)
    {
        $limit = 12;
        $offset = ($p-1)*$limit;
        $type = $this->db->select()->from("product_type")->where('name_link',$cat_name)->get()->row();
        if($type){
            $this->data['type'] = $type;
            $result = $this->home_model->get_by_type($type->id,$offset,$limit);
            $this->data['products'] =   $result['rows'];
            $url			=	site_url('cata/'.$cat_name).'/';
            $total_page = ceil($result['num_rows']/$limit);		
    			
    		$conf = array(
    			'cur_page'			=>	$p,
    			'base_url'			=>	$url,
    			'total_rows'		=>	$total_page,	
    			'cur_class'			=>	'currentpage',
    			'prev_class'		=>	'prevnext',
    			'next_class'		=>	'prevnext',
    			'next_link'			=>	'Tiếp theo',
    			'prev_link'			=>	'Trang trước',
    			'show_total'		=>	'no',
    			'show_first_last'	=>	'no'
    		);
    		
    		$this->pagination->initialize($conf);
    		$this->data['pagnav'] 		= $this->pagination->non_ajax();
        }
        else{
            show_404('');
        }
        $this->data['tpl_file']		=	'home/cata';
		$this->load->view('layout/default',$this->data);
    }
}
